/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jili.ubert.dao;
/**
 *
 * @author ChengJiLi
 */
import com.jili.ubert.dao.sim.TradeSimOrderquene;
import org.hibernate.HibernateException;  
import org.hibernate.Session;  
import org.hibernate.SessionFactory;  
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;  
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
import org.hibernate.cfg.Configuration;  
      
public class NewHibernateUtil {  
private static SessionFactory factory;  
          
    static{  
        try {  
            //Configuration configuration = new Configuration().configure();  
            //StandardServiceRegistry  serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();  
             factory = new Configuration().configure().buildSessionFactory(
			    new StandardServiceRegistryBuilder().build() );
             System.out.println(factory);
      
        } catch (HibernateException e) {  
            e.printStackTrace();  
        }  
    }  
          
    public static SessionFactory getSessionFactory(){  
        return factory;  
    }  
          
    public static Session getOpenSession(){  
        return factory.openSession();  
    }  
          
    public static Session getCurrentSession(){  
        return factory.getCurrentSession();  
    }  
          
    public static void closeSession(Session session){  
        if(session!=null){  
            if(session.isOpen()){  
                session.close();  
            }  
        }  
    }  
    public static void save(TradeSimOrderquene order){
        Session session = NewHibernateUtil.getOpenSession();
        Transaction t = session.beginTransaction();
        session.saveOrUpdate(order);
        session.flush();
        t.commit();
        NewHibernateUtil.closeSession(session);
    }
    public static void update(TradeSimOrderquene order){
        Session session = NewHibernateUtil.getOpenSession();
        Transaction t = session.beginTransaction();
        session.saveOrUpdate(order);
        session.flush();
        t.commit();
        NewHibernateUtil.closeSession(session);
    }
    public static void delte(TradeSimOrderquene order){
        Session session = NewHibernateUtil.getOpenSession();
        Transaction t = session.beginTransaction();
        session.delete(order);
        t.commit();
        session.close(); 
    }
}
